const serverUrl = getApp().serverUrl
let user
Page({
  data: {
    name: '董杰',
    phone: '18234416227',
    address: '山西省太原市尖草坪区学院路三号中北大学',
    price: parseInt('64950'),
    steps: [
      {
        text: '买家下单',
      },
      {
        text: '双方议价',
      },
      {
        text: '买家提货',
      },
      {
        text: '交易完成',
        activeIcon: 'success',
      },
    ],
    stepActive: '3',
    activeNames: ['0'],
    showPrice: false,
    array: ['双方议价', '买家提货', '交易完成'],
    showProgress: false,
    afterPrice: '' // 新价格
  },

  /**
   * 获取订单详情
   */
  getOrderById(id) {
    wx.request({
      url: serverUrl + '/zOrderForm/selectById?id=' + id,
      success: (res) => {
        console.log(res)
        this.setData({
          order: res.data.data.order,
          address: res.data.data.address,
          id
        })
      }
    })
  },

  onLoad: function (options) {
    user = wx.getStorageSync('user')
    // const id = 'f6a6b8cb4a2241baa0ce209f4fea7ed5'
    const id = options.id
    this.getOrderById(id)
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },

  showConfirm1() {
    this.setData({
      showProgress: !this.data.showProgress
    })
  },

  showConfirm2() {
    this.setData({
      showPrice: !this.data.showPrice
    })
  },

  /**
   * 提交价格
   */
  submitPrice() {
    let order = this.data.order
    wx.request({
      url: serverUrl +'/zOrderForm/update',
      method: 'POST',
      data: {
        beforePrice: order.beforePrice,
        afterPrice: this.data.afterPrice,
        id: order.id,
        version: order.version
      },
      success: res => {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
        this.getOrderById(this.data.id)
      }
    })
  },

  /**
   * 输入价格
   */
  priceInput(e) {
    this.setData({
      afterPrice: e.detail
    })
  },

  /**
   * 选择器改变
   */
  bindPickerChange(e) {
    let order = this.data.order
    let statusId = parseInt(e.detail.value) + 1
    console.log("statusId =>", statusId)
    wx.request({
      url: serverUrl +'/zOrderForm/update',
      method: 'POST',
      data: {
        statusId: statusId,
        id: order.id,
        version: order.version
      },
      success: res => {
        console.log(res)
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
        this.getOrderById(this.data.id)
      }
    })
  }
})